package pkg.chemdrm;

import java.util.Hashtable;
import java.util.Map;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;

public class ResourceHandler {
	private Engine 						mEngine;
	private Context						mContext;
	private Font							mDefaultFont; // if we need more we can load them.
	private Hashtable<String, BitmapTextureAtlas> mAtlas;
	private Hashtable<String, TextureRegion> 	mRegions;
	private Hashtable<String, TiledTextureRegion> 	mAnimatedRegions;
	private Hashtable<String, Font> mFonts;

	private TMXLoader	mTileLoader;

	
	public ResourceHandler(final Engine pEngine, final Context pContext)
	{
		mEngine = pEngine;
		mContext = pContext;
		
		mAtlas = new Hashtable<String, BitmapTextureAtlas>();
		mRegions = new Hashtable<String, TextureRegion>();
		mAnimatedRegions = new Hashtable<String, TiledTextureRegion>();
		mFonts = new Hashtable<String, Font>();
		
		
		
		mTileLoader = new TMXLoader(mContext, mEngine.getTextureManager());
		
		// TODO: It may not be necessary to do anything but hand specify this, but just in case this is
		// a flexible class where we can add code to parse a text file, and prepare atlases or 
		// even build an atlas for us.
		manualTextureLoad();
		
	}
	
	private void manualTextureLoad()
	{
		mAtlas.put("MainMenuAtlas", new BitmapTextureAtlas(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("PlayerAtlas", new BitmapTextureAtlas(256, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("EffectsAtlas", new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("DefaultFontAtlas", new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("AFontAtlas", new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA));		
		mAtlas.put("FlaskAtlas", new BitmapTextureAtlas(16, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("blackAtlas", new BitmapTextureAtlas(4, 4, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("FogAtlas", new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("RobotAtlas", new BitmapTextureAtlas(256, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("SlimeAtlas", new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("SlugAtlas", new BitmapTextureAtlas(256, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("FireballAtlas", new BitmapTextureAtlas(128, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA));
		mAtlas.put("GnomeAtlas", new BitmapTextureAtlas(256, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA));

		
		mAtlas.put("DoorAtlas", new BitmapTextureAtlas(512, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA));

		//96x107
		
		//192x321
		//256x512
		
		
		mRegions.put("MainMenu_TitleRegion", BitmapTextureAtlasTextureRegionFactory.createFromAsset(mAtlas.get("MainMenuAtlas"), mContext, "title0.png", 0, 0));
		mRegions.put("glow",  BitmapTextureAtlasTextureRegionFactory.createFromAsset(mAtlas.get("EffectsAtlas"), mContext, "Sprites/glow_small.png", 0, 0));
		mRegions.put("NOTHING", BitmapTextureAtlasTextureRegionFactory.createFromAsset(mAtlas.get("blackAtlas"), mContext, "Sprites/nothing.png", 0, 0));
		mRegions.put("Flask", BitmapTextureAtlasTextureRegionFactory.createFromAsset(mAtlas.get("FlaskAtlas"), mContext, "Sprites/Flask1.png", 0, 0));
		mRegions.put("Fog", BitmapTextureAtlasTextureRegionFactory.createFromAsset(mAtlas.get("FogAtlas"), mContext, "Sprites/Fog.png", 0, 0));
		mRegions.put("Gnome", BitmapTextureAtlasTextureRegionFactory.createFromAsset(mAtlas.get("GnomeAtlas"), mContext, "Sprites/GnomeBig.png", 0, 0));

		mAnimatedRegions.put("player_walk",BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mAtlas.get("PlayerAtlas"), mContext, "Sprites/Player.png", 0, 0, 2, 8));

		mAnimatedRegions.put("Robot",BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mAtlas.get("RobotAtlas"), mContext, "Sprites/Robot.png", 0, 0, 2, 3));
		mAnimatedRegions.put("Slime",BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mAtlas.get("SlimeAtlas"), mContext, "Sprites/Slime.png", 0, 0, 3, 3));
		mAnimatedRegions.put("Slug",BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mAtlas.get("SlugAtlas"), mContext, "Sprites/Slug.png", 0, 0, 2, 3));
		mAnimatedRegions.put("Fireball",BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mAtlas.get("FireballAtlas"), mContext, "Sprites/FireBall.png", 0, 0, 2, 3));


		mAnimatedRegions.put("Door",BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mAtlas.get("DoorAtlas"), mContext, "Sprites/Door.png", 0, 0, 3, 1));

		
		for(BitmapTextureAtlas m : mAtlas.values())
		{
			mEngine.getTextureManager().loadTextures(m);
		}
		
		// the font gets created at runtime so we don't have to create additional images or anything.
		mDefaultFont = new Font(mAtlas.get("DefaultFontAtlas"), Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
		mEngine.getFontManager().loadFont(mDefaultFont);
		
		mFonts.put("DEFAULT", mDefaultFont);
		Font font = new Font(mAtlas.get("AFontAtlas"), Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 12, true, Color.WHITE);
		mEngine.getFontManager().loadFont(font);
		
		//Font font1 = new Font(mAtlas.get("AFontAtlas"), Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20, true, Color.WHITE);
		//mEngine.getFontManager().loadFont(font1);
		
		
		mFonts.put("Inventory", font);
		
		//Added by Shane for the inventory buttons
		//mFonts.put("InventoryButtons", font1);
	}
	
	public TextureRegion getRegion(final String pRegionName)
	{
		// TODO: Error handling!
		return mRegions.get(pRegionName);
		
	}
	
	public Font getFont()
	{
		return mDefaultFont;	
	}
	
	public Font getFont(final String pFontName)
	{
		System.err.println("Querying 1 of " + mFonts.size() + " fonts");
		for(String s : mFonts.keySet())
		{
			System.err.println("Font is called " + s);
		}
		
		if(mFonts.containsKey(pFontName))
		{
			return mFonts.get(pFontName);
		}
		System.err.println("Returning a null font since we can't find " + pFontName);
		return null;
	}
	
	public TiledTextureRegion getAnimatedRegion(final String pRegionName)
	{
		return mAnimatedRegions.get(pRegionName);
	}
	
	public TMXTiledMap getMap(final String pFilename)
	{
		try {
			System.out.println("Loading " + pFilename);
			return mTileLoader.loadFromAsset(mContext, pFilename);
		} catch (TMXLoadException e) {
			System.out.println("Problem loading... " + pFilename);
			e.printStackTrace();
			return null;
		}
	}
	
}
